<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/10/15
 * Time: 19:00
 */

namespace app\common\mysic\order;


use app\common\model\Order;
use app\common\mysic\MysicBase;
use think\Exception;
use think\Model;
use app\common\model\divide\Store;
use app\common\model\Wxapp;
use app\common\model\Wxsetting;

class StoreDividendOrderOp extends MysicBase implements OrderOpInterface
{
    protected $model;
    protected $order;
    protected $issue;
    protected $bonus;

    protected $bonusSetting;
    protected $storeEntity;

    public function setModel(Model $model)
    {
        if(!($model instanceof Store) || empty($model)) {
            throw new Exception('设置的商户分账订单模型不合法');
        }
        $this->model = $model;
        return $this;
    }

    public function getModel()
    {
        return $this->model;
    }

    public function setOrder(Model $order)
    {
        if(!($order instanceof Order) || empty($order)) {
            throw new Exception('设置的用户订单模型不合法');
        }
        $this->order = $order;
        return $this;
    }

    public function getOrder()
    {
        return $this->order;
    }

    public function setMiniProgramEntity(Model $entity)
    {
        if(!($entity instanceof Wxapp) || empty($entity)) {
            throw new Exception('设置的小程序模型不合法');
        }
        $this->storeEntity = $entity;
        return $this;
    }

    public function setBonusSettingEntity(Model $bonusSetting)
    {
        $this->bonusSetting = $bonusSetting;
        return $this;
    }

    public function setTotalBonus($bonus)
    {
        $this->bonus = $bonus;
        return $this;
    }

    public function setIssue($issue)
    {
        if($issue < 0) {

            $issue = 0;
        }
        $this->issue = $issue;
        return $this;
    }

    public function  create()
    {
        $this->model->id = null;

        $this->model->account = $this->storeEntity->mchid;
        $this->model->order_no =$this->order->order_no;
        $this->model->wxapp_id = $this->order->wxapp_id;
        $this->model->agent_id = $this->storeEntity->agent_id;
        $this->model->payment = $this->order->pay_price;
        $this->model->transaction_id = $this->order->transaction_id;
        $this->model->status = 0;
        $this->model->create_time = time();
        $this->model->periods_num = $this->issue;
        $this->model->amount = bcadd(bcmul(strval($this->order->pay_price),'0.01', 2),  $this->bonus, 2);

        $this->model->isUpdate(false);
        return $this->model->save();
    }

    public function failure($msg)
    {
        $data = [
            'status' => 2,
            'update_time' => time(),
            'fail_reason' => $msg
        ];
        return $this->update($data);

    }

    public function success()
    {
        $data = [
            'status' => 1,
            'update_time' => time(),
            'fail_reason' => '分账成功'
        ];
        return $this->update($data);
    }

    public function updateAmount(array $updateParam)
    {
        return $this->model->save($updateParam);

    }


    protected function update($data)
    {
        return $this->model->isUpdate(true)->save($data);

    }
}